from datetime import datetime
from pydantic import Field, field_validator, ConfigDict
from .base import BaseSchema

class WarehouseGoodsBase(BaseSchema):
    """仓库商品关系基础模型"""
    warehouse_id: int = Field(..., description="仓库ID", gt=0)
    goods_id: int = Field(..., description="商品ID", gt=0)
    quantity: int = Field(default=0, description="当前库存数量", ge=0)
    min_quantity: int = Field(default=0, description="最小库存", ge=0)
    max_quantity: int = Field(default=0, description="最大库存", gt=0)

    @field_validator('max_quantity')
    @classmethod
    def validate_max_quantity(cls, v, info):
        """验证最大库存大于最小库存"""
        if 'min_quantity' in info.data and v < info.data['min_quantity']:
            raise ValueError('最大库存必须大于最小库存')
        return v

    @field_validator('quantity')
    @classmethod
    def validate_quantity(cls, v, info):
        """验证库存数量在最大最小值之间"""
        if 'min_quantity' in info.data and v < info.data['min_quantity']:
            raise ValueError('库存数量不能小于最小库存')
        if 'max_quantity' in info.data and v > info.data['max_quantity']:
            raise ValueError('库存数量不能超过最大库存')
        return v

    model_config = ConfigDict(
        json_schema_extra={
            "example": {
                "warehouse_id": 1,
                "goods_id": 1,
                "quantity": 100,
                "min_quantity": 10,
                "max_quantity": 1000
            }
        }
    )

class WarehouseGoodsCreate(WarehouseGoodsBase):
    """创建仓库商品关系请求模型
    
    Attributes:
        warehouse_id (int): 仓库ID，必填，大于0
        goods_id (int): 商品ID，必填，大于0
        quantity (int): 初始库存数量，默认0，不能小于0
        min_quantity (int): 最小库存警戒值，默认0，不能小于0
        max_quantity (int): 最大库存限制，必须大于min_quantity
    """
    model_config = ConfigDict(
        json_schema_extra={
            "example": {
                "warehouse_id": 1,
                "goods_id": 1,
                "quantity": 100,
                "min_quantity": 10,
                "max_quantity": 1000
            }
        }
    )

class WarehouseGoodsUpdate(BaseSchema):
    """更新仓库商品关系请求模型
    
    Attributes:
        quantity (int | None): 更新的库存数量，可选，不能小于0
        min_quantity (int | None): 更新的最小库存警戒值，可选，不能小于0
        max_quantity (int | None): 更新的最大库存限制，可选，必须大于min_quantity
    """
    quantity: int | None = Field(None, description="当前库存数量", ge=0)
    min_quantity: int | None = Field(None, description="最小库存", ge=0)
    max_quantity: int | None = Field(None, description="最大库存", gt=0)

    @field_validator('max_quantity')
    @classmethod
    def validate_max_quantity(cls, v, info):
        """验证最大库存大于最小库存"""
        if v is not None and 'min_quantity' in info.data and info.data['min_quantity'] is not None:
            if v < info.data['min_quantity']:
                raise ValueError('最大库存必须大于最小库存')
        return v

    @field_validator('quantity')
    @classmethod
    def validate_quantity(cls, v, info):
        """验证库存数量在最大最小值之间"""
        if v is not None:
            if 'min_quantity' in info.data and info.data['min_quantity'] is not None:
                if v < info.data['min_quantity']:
                    raise ValueError('库存数量不能小于最小库存')
            if 'max_quantity' in info.data and info.data['max_quantity'] is not None:
                if v > info.data['max_quantity']:
                    raise ValueError('库存数量不能超过最大库存')
        return v

    model_config = ConfigDict(
        json_schema_extra={
            "example": {
                "quantity": 100,
                "min_quantity": 10,
                "max_quantity": 1000
            }
        }
    )

class WarehouseGoodsResponse(WarehouseGoodsBase):
    """仓库商品关系响应模型"""
    id: int
    create_at: datetime
    update_at: datetime
    warehouse_name: str = Field(..., description="仓库名称")
    warehouse_address: str = Field(..., description="仓库地址")
    goods_name: str = Field(..., description="商品名称")
    goods_code: str | None = Field(None, description="商品编码")
    goods_category: str | None = Field(None, description="商品类别")

    model_config = ConfigDict(
        json_schema_extra={
            "example": {
                "id": 1,
                "warehouse_id": 1,
                "warehouse_name": "华北仓库",
                "warehouse_address": "北京市朝阳区",
                "goods_id": 1,
                "goods_name": "苹果笔记本电脑",
                "goods_code": "APPLE-001",
                "goods_category": "电脑",
                "quantity": 100,
                "min_quantity": 10,
                "max_quantity": 1000,
                "create_at": "2024-01-01T00:00:00",
                "update_at": "2024-01-01T00:00:00"
            }
        }
    )